//
//  HomeFuKuanShareViewController.m
//  shopping
//
//  Created by 马田成 on 2017/9/30.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "HomeFuKuanShareViewController.h"
#import "SGQRCodeTool.h"
@interface HomeFuKuanShareViewController ()
@property (nonatomic, strong) UILabel *topLabel;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImageView *imageHeadView;
@end

@implementation HomeFuKuanShareViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];

    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitle:@"收款"];

    
    [self leftNavHide];
    self.imageHeadView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64)];
    
    [self.view addSubview:self.imageHeadView];
    self.imageHeadView.image = [UIImage imageNamed:@"qrcodebg"];
    
    // 生成二维码(中间带有图标)
    [self setupGenerate_Icon_QRCode];
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, screenScale(20), screenScale(20));
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"back_black"] forState:UIControlStateNormal];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"back_black"] forState:UIControlStateSelected];
    [leftBtn addTarget:self action:@selector(leftNavItemClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBarBtn;
    
    
}


-(void)leftNavItemClick{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - - - 中间带有图标二维码生成
- (void)setupGenerate_Icon_QRCode {
    
    UIView *threadView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), 1)];
    threadView.backgroundColor = getColor(@"cccccc");
    [self.view addSubview:threadView];
    
    self.topLabel = [[UILabel alloc] init];
    [self.view addSubview:self.topLabel];
    
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(28);
        make.centerX.equalTo(self.view);
        make.height.offset(14);
        
    }];
    self.topLabel.text = @"请扫描下面二维码收取";
    self.topLabel.font = [UIFont systemFontOfSize:14];
    
    
    
    // 1、借助UIImageView显示二维码
    self.imageView = [[UIImageView alloc] init];
    [self.view addSubview:self.imageView];
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topLabel.mas_bottom).with.offset(10);
        make.left.equalTo(self.view).with.offset(89);
        make.right.equalTo(self.view).with.offset(-89);
        make.height.equalTo(self.imageView.mas_width);
        
        
    }];
    
    //http://112.74.36.208
    CGFloat scale = 0.2;
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
 //   NSString *strData = [str3 stringByAppendingString:invitation];
    
   //  2、将最终合得的图片显示在UIImageView上
    self.imageView.image = [SGQRCodeTool SG_generateWithLogoQRCodeData:token logoImageName:@"商城ICON" logoScaleToSuperView:scale];
    
//    UILabel *labelPhone = [[UILabel alloc] init];
//    [self.view addSubview:labelPhone];
//    labelPhone.text = @"长按保存二维码";
//    labelPhone.textColor =[UIColor whiteColor];
//    labelPhone.font = [UIFont systemFontOfSize:15];
//    [labelPhone mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.imageView.mas_bottom).with.offset(4);
//        make.centerX.equalTo(self.view.mas_centerX);
//        make.height.offset(20);
//        
//    }];
//    
//    
//    //1.创建长按手势
//    UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(imglongTapClick:)];
//    
//    //2.开启人机交互
//    self.imageView.userInteractionEnabled = YES;
//    
//    //3.添加手势
//    [self.imageView addGestureRecognizer:longTap];
//    
    
    
}

#pragma mark 长按手势弹出警告视图确认
-(void)imglongTapClick:(UILongPressGestureRecognizer*)gesture

{
    
    if(gesture.state==UIGestureRecognizerStateBegan)
        
    {
        
        UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"保存图片" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"取消保存图片");
        }];
        
        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"确认保存图片");
            
            // 保存图片到相册
            UIImageWriteToSavedPhotosAlbum(self.imageView.image,self,@selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),nil);
            
            //            UIImageWriteToSavedPhotosAlbum(<#UIImage * _Nonnull image#>, <#id  _Nullable completionTarget#>, <#SEL  _Nullable completionSelector#>, <#void * _Nullable contextInfo#>)
            
        }];
        
        [alertControl addAction:cancel];
        [alertControl addAction:confirm];
        
        [self presentViewController:alertControl animated:YES completion:nil];
        
    }
    
}

#pragma mark 保存图片后的回调
- (void)imageSavedToPhotosAlbum:(UIImage*)image didFinishSavingWithError:  (NSError*)error contextInfo:(id)contextInfo

{
    
    NSString*message =@"呵呵";
    
    if(!error) {
        
        message =@"成功保存到相册";
        
        UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertControl addAction:action];
        
        [self presentViewController:alertControl animated:YES completion:nil];
        
    }else
        
    {
        
        message = [error description];
        
        
        UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertControl addAction:action];
        
        [self presentViewController:alertControl animated:YES completion:nil];
        
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
